package peishui.fragments;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xkh.xkh.R;

import peishui.views.adapters.CannelAdapter;
import shuiyuqing.fragments.WaterGateDialogFragment;
import shuiyuqing.network.beans.IrrBAWaterGate;
import shuiyuqing.network.beans.IrrBTCanalDetail;


/**
 * 选择渠道
 * Created by 凯 on 2015/11/7.
 */
public class CannelDialogFragment extends DialogFragment {

    Delegate delegate;

    ListView lv_cannel;
    CannelAdapter cannelAdapter;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.fragment_cannel, null);
        initview(view);
        return view;
    }

    // 初始化
    private void initview(View view) {
        lv_cannel = (ListView) view.findViewById(R.id.id_lv_sel_cannel);
        cannelAdapter = new CannelAdapter();
        lv_cannel.setAdapter(cannelAdapter);

        lv_cannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IrrBTCanalDetail detail = (IrrBTCanalDetail) view.getTag(R.id.id_cannel);
                if (delegate != null)
                {
                    delegate.select(detail);
                }
                dismiss();

            }
        });

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof Delegate)
        {
            delegate = (Delegate) activity;
        }
    }

    // 回调主界面
    public static interface Delegate {
        // 告知activity已选择渠道
        public void select(IrrBTCanalDetail canalDetail);
    }
}
